package com.bsj.travel.security.auth;

import com.bsj.travel.def.global.JsonResultEnum;
import com.bsj.travel.exception.ApiException;
import com.bsj.travel.util.AesUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.TreeMap;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description AuthOpenApi
 * @time 2024/4/7 16:15
 */
@Slf4j
@Component
public class AuthOpenApi {

    private final int minute = 1000 * 60 * 5;

    @Autowired
    private AuthProduct authProduct;

    /**
     * 超时校验
     *
     * @param timestamp
     * @return
     * @author ljx
     * @time 2024/4/7 16:18
     */
    private void timeoutCheck(String timestamp) {
        if (System.currentTimeMillis() - Long.parseLong(timestamp) > minute) {
            throw new ApiException(JsonResultEnum.REQUEST_TIMEOUT);
        }
    }

    /**
     * 获取公共参数
     *
     * @param request
     * @return
     * @author ljx
     * @time 2024/4/7 17:28
     */
    public TreeMap<String, String> getCommonParam(HttpServletRequest request) {
        String appId = request.getHeader("appId");
        String sign = request.getHeader("sign");
        String timestamp = request.getHeader("timestamp");
        String signatureNonce = request.getHeader("signatureNonce");
        log.info("appId-->{},sign-->{},timestamp-->{},signatureNonce-->{}", appId, sign, timestamp, signatureNonce);
        if (StringUtils.isAnyBlank(appId, sign, timestamp, signatureNonce)) {
            log.info("请求头参数不全appId-->{},sign-->{},timestamp-->{},signatureNonce-->{}", appId, sign, timestamp, signatureNonce);
            throw new ApiException(JsonResultEnum.COMMON_PARAM_IS_NULL);
        }
        timeoutCheck(timestamp);
        // 默认按照key的字典顺序(ASCLL码)排序
        TreeMap<String, String> map = new TreeMap<>();
        // 公共参数
        map.put("appId", appId);
        map.put("sign", sign);
        map.put("timestamp", timestamp);
        map.put("signatureNonce", signatureNonce);
        return map;
    }

    /**
     * 验证签名是否正确
     *
     * @return
     * @author ljx
     * @time 2024/4/7 17:34
     */
    public void signCheck(TreeMap<String, String> map, String productKey) {
        String appId = map.get("appId");
        String secretKey = authProduct.permissionCheck(appId, productKey);
        String sign = map.get("sign");
        map.remove("sign");
        StringBuilder builder = new StringBuilder();
        map.forEach((key, value) -> {
            builder.append(key).append(value);
        });
//        log.info("加密前参数{}", builder.toString());
//        log.info("请求头内容{}", sign);
//        log.info("加密后内容{}", AesUtils.encrypt(secretKey, builder.toString()));
        if (!sign.equals(AesUtils.encrypt(secretKey, builder.toString()))) {
            throw new ApiException(JsonResultEnum.NOT_ERR_OPEN_AUTH);
        }
    }

    public static void main(String[] args) {
        TreeMap<String, String> map = new TreeMap<>();
        // 公共参数
        map.put("appId", "CctyXKG50C4n4jgSKQXKRXEA");
        map.put("timestamp", "1712633599000");
        map.put("signatureNonce", "sdfjk3398u9wer3904");


        map.put("deviceName", "sadfkh89723");
        map.put("productKey", "b1yxmipguze");
        StringBuilder builder = new StringBuilder();
        map.forEach((key, value) -> {
            builder.append(key).append(value);
        });
        System.out.println("加密前参数-->" + builder.toString());
        String sign = AesUtils.encrypt("HaE9hj8IGF2UzYlvPCxllT0u", builder.toString());
        System.out.println("sign-----" + sign);
    }

}
